package com.tcm.utils;

import com.tcm.entity.dto.DayRange;

import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 * @author Guqier
 * @version 1.0
 * @date 2023/9/12 21:33
 */
public class DateTimeUtil {

    /**
     * 根据当前时间获取时间范围
     * @param range 时间范围，单位天
     * @return 时间范围DTO
     */
    public static DayRange dayRange(Integer range){
        // 获取当前最后时间
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        Date endTime = calendar.getTime();
        // 获取N天前开始时间
        calendar.add(Calendar.DATE, -range);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date startTime = calendar.getTime();
        return new DayRange(startTime, endTime);
    }

}
